#!/usr/bin/python3
# coding: utf-8


from py2neo import Graph


class AnswerSearcher:
    def __init__(self):
        self.g = Graph(
            host="192.168.162.202",
            http_port=7474,
            user="neo4j",
            password="xxxxxx")
        self.num_limit = 20

    '''执行cypher查询，并返回相应结果'''

    def search_main(self, sqls):
        final_answers = []
        for sql_ in sqls:
            question_type = sql_['question_type']
            queries = sql_['sql']
            answers = []
            for query in queries:
                ress = self.g.run(query).data()
                answers += ress
            final_answer = self.answer_prettify(question_type, answers)
            if final_answer:
                final_answers.append(final_answer)
        return final_answers

    '''根据对应的qustion_type，调用相应的回复模板'''

    def answer_prettify(self, question_type, answers):
        final_answer = []
        if not answers:
            return ''
        if question_type == 'error_err_cause':
            desc = [i['n.name'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}的故障可能由以下原因造成：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'err_cause_error':
            desc = [i['m.name'] for i in answers]
            subject = answers[0]['n.name']
            final_answer = '产生{0}影响的故障有：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'error_server':
            desc = [i['n.name'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}相关服务器为：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))
        elif question_type == 'server_error':
            desc = [i['m.name'] for i in answers]
            subject = answers[0]['n.name']
            final_answer = '服务器相关{0}故障为：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'error_link_module':
            desc = [i['n.name'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}相关模块为：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))
        elif question_type == 'link_module_error':
            desc = [i['m.name'] for i in answers]
            subject = answers[0]['n.name']
            final_answer = '{0}相关模块故障为：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'error_desc':
            desc = [i['m.desc'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0},指的是这个意思：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'error_err_effect':
            desc = [i['n.name'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}产生影响有：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))
        elif question_type == 'err_effect_error':
            desc = [i['m.name'] for i in answers]
            subject = answers[0]['n.name']
            final_answer = '{0}该影响产生故障有：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'error_solve_way':
            desc = [i['n.name'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}通常的解决方案包括：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'solve_way_error':
            desc = [i['m.name'] for i in answers]
            subject = answers[0]['n.name']
            final_answer = '{0}解决的故障有{1},可以试试'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'error_check_way':
            desc = [i['n.name'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}通常可以通过以下方式来排查：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'check_way_error':
            desc = [i['m.name'] for i in answers]
            subject = answers[0]['n.name']
            final_answer = '通常可以通过{0}检查出来的故障有{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))
        elif question_type == 'null':
            final_answer = '请输入需要咨询的内容，你可以输入故障名称、如何解决***故障、***故障如何排查等'

        return final_answer


if __name__ == '__main__':
    searcher = AnswerSearcher()
